package com.hs.spring.jdbc;

import com.hs.spring.jdbc.config.SpringConfiguration;
import com.hs.spring.jdbc.dao.UserDao;
import com.hs.spring.jdbc.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: songhao
 * @Date: 2021/9/5
 * @Description: TODO
 */
public class TestJdbc {

    @Test
    public void testQueryUser(){
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);

        UserDao userDao = applicationContext.getBean(UserDao.class);
        System.out.println(userDao.getById(1));
    }

    @Test
    public void testQueryUserList(){
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);

        UserDao userDao = applicationContext.getBean(UserDao.class);
        List<User> userList = userDao.findAll();
        for(User user : userList){
            System.out.println(user);
        }
    }

    @Test
    public void testUpdate(){
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);

        UserDao userDao = applicationContext.getBean(UserDao.class);
        User user = new User();
        user.setId(1);
        user.setUsername("zhangsan");
        //更新用户
        userDao.update(user);
    }

    @Test
    public void testDelete(){
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);

        UserDao userDao = applicationContext.getBean(UserDao.class);
        userDao.delete(1);
    }

    @Test
    public void testTransaction(){
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);


        UserDao userDao = applicationContext.getBean(UserDao.class);
        DataSource dataSource = applicationContext.getBean(DataSource.class);

        User user1 = new User(1,"zhangsan","admin",900);
        User user2 = new User(2,"lisi","admin",10000);
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try{
            conn.setAutoCommit(false);
            //设置转账金额
            int tranMoney = 1314;
            user1.setMoney(user1.getMoney() - tranMoney);
            user2.setMoney(user2.getMoney() + tranMoney);
            userDao.updateWithTransaction(user2, conn);
            userDao.updateWithTransaction(user1, conn);
            //提交事务
            conn.commit();
        }catch(Exception ex){
            ex.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
